package burubaka.common;

import java.io.IOException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slim3.util.TimeZoneLocator;

import burubaka.BurubakaConstants;
import burubaka.model.PayOff;
import burubaka.model.RaceInfo;

/**
 * @author tk
 * 
 */
public class KeibaNiftyUtil {

	private final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	static {
		SDF.setTimeZone(TimeZoneLocator.get());
	}

	/**
	 * @param url
	 * @param BASE_URL
	 * @return URLText
	 * @throws Exception
	 */
	public static String getURLText(String url, String BASE_URL) throws Exception {
		String kaisaiNo = KeibaNiftyUtil.getKaisaiNo(url);
		if (kaisaiNo.equals("")) {
			return "";
		}
		String raceNo = getRaceNo(url);
		if (raceNo.equals("")) {
			return "";
		}
		return getURLText(kaisaiNo, raceNo, BASE_URL);
	}

	/**
	 * @param kaisaiNo
	 * @param raceNo
	 * @param baseUrl
	 * @return URLText
	 * @throws Exception
	 */
	public static String getURLText(String kaisaiNo, String raceNo, String baseUrl) throws Exception {
		return BurubakaUtil.getURLText(getURL(kaisaiNo, raceNo, baseUrl));
	}

	/**
	 * urlを取得する
	 * 
	 * @param kaisaiNo
	 * @param raceNo
	 * @param baseUrl
	 * @return URL
	 * @throws Exception
	 */
	public static String getURL(String kaisaiNo, String raceNo, String baseUrl) throws Exception {
		return baseUrl.replace("*", kaisaiNo).replace("?", raceNo);
	}

	/**
	 * トップページのソースを取得します。
	 * 
	 * @return 本文
	 * @throws MalformedURLException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static String getTopPageStr() throws MalformedURLException, IOException, InterruptedException {
		return BurubakaUtil.getURLText(BurubakaConstants.URL_KEIBA_NIFTY_COM);
	}

	/**
	 * トップページのレース名一覧を取得します。
	 * 
	 * @param str
	 * @return receNmae
	 */
	public static String getTopPageRaceNameArea(String str) {
		return BurubakaUtil.substring("summary=\"レース情報　開催一覧\">", "</table>", str);
	}

	/**
	 * 対象レースのURLを取得します。
	 * 
	 * @param racename
	 * @param info
	 * @return receUrl
	 */
	public static String getRaceUrl(String racename, String info) {

		if (racename == null) {
			return null;
		}

		String strTag = "<dd>";
		String endTag = "</dd>";

		int start = 0;

		while (true) {
			int begin = info.indexOf(strTag, start);
			if (begin < 0) {
				break;
			}
			begin = begin + strTag.length();
			int end = info.indexOf(endTag, begin);
			if (end < 0) {
				break;
			}
			start = end;

			String race = info.substring(begin, end);
			race = race.replace("href=\"", "href=\"" + BurubakaConstants.URL_KEIBA_NIFTY_COM);
			String name = BurubakaUtil.substring(">", "</a>", race);
			if (name.indexOf(racename) >= 0) {
				return BurubakaUtil.substring("\"", "\"", race);
			}
		}

		return null;
	}

	/**
	 * レース情報を取得する
	 * 
	 * @param text
	 * @param url
	 * @return RaceInfo
	 * @throws Exception
	 */
	public static RaceInfo getRaceInfo(String text, String url) throws Exception {
		String raceInfoText = BurubakaUtil.substring("<div id=\"resultTtl\" class=\"noBg\">", "</div>", text);
		String raceDate = getRaceDate(raceInfoText);
		// レース情報取得
		return getRaceInfo(url, raceInfoText, raceDate);
	}

	/**
	 * @param url
	 * @param raceInfoText
	 * @param raceDate
	 * @return RaceInfo
	 * @throws Exception
	 */
	public static RaceInfo getRaceInfo(String url, String raceInfoText, String raceDate) throws Exception {

		RaceInfo raceInfo = new RaceInfo();
		raceInfo.setRaceInfo3(raceDate);

		String rDate = raceDate.substring(0, 10).replaceAll("年", "/").replaceAll("月", "/");
		raceInfo.setRaceDate(rDate);

		raceInfo.setStartTime(SDF.parse(rDate + " 00:00:00"));

		String raceName = BurubakaUtil.substring("<h3>", "</h3>", raceInfoText);
		raceInfo.setRaceName(raceName);

		String raceInfo1 = BurubakaUtil.substring("<p class=\"info1\">", "</p>", raceInfoText);
		raceInfo.setRaceInfo1(raceInfo1);

		String raceInfo2 = BurubakaUtil.substring("<p class=\"info2\">", "</p>", raceInfoText);
		raceInfo.setRaceInfo2(raceInfo2);

		String kaisaiNo = getKaisaiNo(url);
		raceInfo.setKaisaiNo(kaisaiNo);

		String raceNo = getRaceNo(url);
		raceInfo.setRaceNo(raceNo);

		raceInfo.setRaceURL(url);

		return raceInfo;
	}

	/**
	 * レース日付を取得する
	 * 
	 * @param text
	 * @param url
	 * @return RaceInfo
	 * @throws Exception
	 */
	public static String getRaceDate(String text) throws Exception {
		// レース情報取得
		return BurubakaUtil.substring("<p class=\"date\">", "</p>", text);
	}

	/**
	 * 開始日付を取得する
	 * 
	 * @param text
	 * @param url
	 * @return RaceInfo
	 * @throws Exception
	 */
	public static Date getStartDate(String text) throws Exception {
		// レース情報取得
		String sDate = BurubakaUtil.substring("<p class=\"date\">", "</p>", text);
		String yMd = BurubakaUtil.substring("", "日", sDate).replaceAll("年", "/").replaceAll("月", "/").trim();
		if (StringUtils.isEmpty(yMd)) {
			return null;
		}
		String hm = BurubakaUtil.substring("R　", "発走", sDate).trim();
		if (StringUtils.isEmpty(hm)) {
			return null;
		}
		return SDF.parse(yMd + " " + hm);
	}

	/**
	 * 締切り日を取得する
	 * 
	 * @param text
	 * @param url
	 * @return RaceInfo
	 * @throws Exception
	 */
	public static Date getDeadline(String text) throws Exception {
		// レース情報取得
		return BurubakaUtil.getDeadline(getStartDate(text));
	}

	/**
	 * @param url
	 * @return kaisaiNo
	 * @throws Exception
	 */
	public static String getKaisaiNo(String url) throws Exception {
		String kaisaiNo = BurubakaUtil.substring("kaisai_", "/", url);
		if (kaisaiNo == null) {
			return "";
		}
		return kaisaiNo;
	}

	/**
	 * @param url
	 * @return raceNo
	 * @throws Exception
	 */
	public static String getRaceNo(String url) throws Exception {
		String raceNo = BurubakaUtil.substring("raceNo_", "/", url);
		if (raceNo == null) {
			return "";
		}
		return raceNo;
	}

	public static ArrayList<String> getMemoList(ArrayList<String> horseNamesA) {
		return BurubakaUtil.substringList("<em class='notice'>", "</em>", horseNamesA);
	}

	public static ArrayList<String> getJockeyAList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"jockey\">", "</td>", dataTbl);
	}

	public static ArrayList<String> getWeightList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"kinryou\">", "</td>", dataTbl);
	}

	public static ArrayList<String> getSexAgeList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"age\">", "</td>", dataTbl);
	}

	public static ArrayList<String> getHorseNameAList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"bamei\">", "</td>", dataTbl);
	}

	public static ArrayList<String> getWakuList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"waku no", "\">", dataTbl);
	}

	public static ArrayList<String> getHorseNoList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"uma\">", "</td>", dataTbl);
	}

	public static ArrayList<String> getFinishPointList(String dataTbl) {
		return BurubakaUtil.substringList("<td class=\"rank\">", "</td>", dataTbl);
	}

	/**
	 * URLテキストを取得する
	 * 
	 * @param raceInfo
	 * @return URLText
	 * @throws Exception
	 */
	public static String getResultURLText(RaceInfo raceInfo) throws Exception {
		String kaisaiNo = raceInfo.getKaisaiNo();
		if (StringUtils.isEmpty(kaisaiNo)) {
			kaisaiNo = KeibaNiftyUtil.getKaisaiNo(raceInfo.getRaceURL());
		}
		String raceNo = raceInfo.getRaceNo();
		if (StringUtils.isEmpty(raceNo)) {
			raceNo = getRaceNo(raceInfo.getRaceURL());
		}
		String text = getURLText(kaisaiNo, raceNo, BurubakaConstants.BASE_URL_RESULT_CURRENT);
		if (StringUtils.isEmpty(text)) {
			text = getURLText(kaisaiNo, raceNo, BurubakaConstants.BASE_URL_RESULT_PAST);
		}
		return text;
	}

	/**
	 * 配当情報を取得する
	 * 
	 * @param text
	 * @return 配当情報
	 */
	public static PayOff getPayOff(String text) {
		PayOff ret = new PayOff();

		String dataTbl = BurubakaUtil.substring("class=\"refundList leftSide\">", "</div>", text);
		ArrayList<String> kekka = BurubakaUtil.substringList("<td class=\"kekka\">", "</td>", dataTbl);
		ArrayList<String> haitou = BurubakaUtil.substringList("<td class=\"haitou\">", "</td>", dataTbl);

		// 単勝セット
		ret.setWinNo(kekka.get(0));
		ret.setWin(BurubakaUtil.parserLongHaitou(haitou.get(0)));

		// 複勝
		ret.setPlace1No(kekka.get(1));
		ret.setPlace1(BurubakaUtil.parserLongHaitou(haitou.get(1)));
		ret.setPlace2No(kekka.get(2));
		ret.setPlace2(BurubakaUtil.parserLongHaitou(haitou.get(2)));
		ret.setPlace3No(kekka.get(3));
		ret.setPlace3(BurubakaUtil.parserLongHaitou(haitou.get(3)));

		// 枠連
		ret.setBracketQuinellaNo(kekka.get(4));
		ret.setBracketQuinella(BurubakaUtil.parserLongHaitou(haitou.get(4)));

		// 馬複
		ret.setQuinellaNo(kekka.get(5));
		ret.setQuinella(BurubakaUtil.parserLongHaitou(haitou.get(5)));

		dataTbl = BurubakaUtil.substring("class=\"refundList rightSide\">", "</div>", text);
		kekka = BurubakaUtil.substringList("<td class=\"kekka\">", "</td>", dataTbl);
		haitou = BurubakaUtil.substringList("<td class=\"haitou\">", "</td>", dataTbl);

		// ワイド
		ret.setQuinellaPlace1No(kekka.get(0));
		ret.setQuinellaPlace1(BurubakaUtil.parserLongHaitou(haitou.get(0)));
		ret.setQuinellaPlace2No(kekka.get(1));
		ret.setQuinellaPlace2(BurubakaUtil.parserLongHaitou(haitou.get(1)));
		ret.setQuinellaPlace3No(kekka.get(2));
		ret.setQuinellaPlace3(BurubakaUtil.parserLongHaitou(haitou.get(2)));

		// 馬単
		ret.setExactaNo(kekka.get(3));
		ret.setExacta(BurubakaUtil.parserLongHaitou(haitou.get(3)));

		// 3連複
		ret.setTrioNo(kekka.get(4));
		ret.setTrio(BurubakaUtil.parserLongHaitou(haitou.get(4)));

		// 3連単
		ret.setTrifectaNo(kekka.get(5));
		ret.setTrifecta(BurubakaUtil.parserLongHaitou(haitou.get(5)));

		return ret;
	}

}
